package com.miyu.test.controller;

import com.alibaba.fastjson.JSONObject;

import com.miyu.common.utils.ehcache.EhcacheUtil;
import com.miyu.test.service.testService;
import org.ehcache.Cache;
import org.ehcache.core.EhcacheManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("test")
public class testController {

    @Autowired
    private static EhcacheManager cacheManager;
    @Autowired
    private testService ts;

    @RequestMapping("save")
    public JSONObject test(@RequestBody JSONObject param){
//        new ehcacheTest();

        // 获取初始化的缓存对象
        Cache<String, String> mineCache = cacheManager.getCache("defaultCache", String.class, String.class);

        String s = mineCache.get("defaultCache");
        System.out.println(">>>>>>>>>>>>>>sss"+s);
        // 创建测试内容
        StringBuilder strTemp = new StringBuilder("aaaaaaaaaaaaaaaaaaaaaaaaaaa");
        JSONObject jo = new JSONObject();
        jo.put("ys","css");
       /* // 存入第1条数据
        mineCache.put("key", strTemp.toString());
        // 取出并输出
        System.out.println("key:" + mineCache.get("key"));
        strTemp = new StringBuilder("bbbbbbbbbbbbbbbbbbbbbbbb");
        // 存入第2条数据
        mineCache.put("key2", strTemp.toString());
        // 取出并输出
        mineCache.containsKey("key1");
        System.out.println("key2:" + mineCache.get("key2"));
        // 取出并输出第一条数据,因为heap的个数设置为1所以当存入第2条数据时,第一条会被淘汰
        System.out.println("key1:" + mineCache.get("key1"));*/
        // 关闭ehcache
        cacheManager.close();

        //ts.testSave(param);
        JSONObject rs = new JSONObject();
        rs.put("pst","返回素具测试不");
        return  rs;
    }

    @RequestMapping("ehcache")
    public String ehcache(){
        String str ="aaaaaaaa";

//        EhcacheUtils.set("test","key","测试数据时ddddddddddddd长ddaaa");
//        System.out.println(EhcacheUtils.get("test","key"));

        //EhcacheUtil.getInstance().put("bookEhcache","key","testasdsds",15);
        System.out.println(EhcacheUtil.getInstance().get("bookEhcache","key"));
        return  str;
    }


    @RequestMapping("cacheEvict")
    public JSONObject cacheEvict(){
      return   ts.cacheEvict();
    }
}
